package scau.osdesign.view;

import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import scau.osdesign.LaunchMain;
import scau.osdesign.model.Fork;
import scau.osdesign.model.Philosopher;
import scau.osdesign.model.PhilosopherStates;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Vector;

public class ViewArranger{
    public static String THINKING_IMAGE_URL  = "src/main/resources/scau/img/thinking-doge.png";
    public static String EATING_IMAGE_URL    = "src/main/resources/scau/img/cat-eating.jpg";
    public static String WAITING_IMAGE_URL   = "src/main/resources/scau/img/waiting.png";
    public static Image imageThinking;
    public static Image imageEating;
    public static Image imageWaiting;

    public static boolean isInit = false;

    public static void updateState(Philosopher[] philosophers, Fork[] forks, Vector<String> vector){
        try{
            if(!isInit){//只加载一次图片
                FileInputStream imageInput;
                imageInput = new FileInputStream(THINKING_IMAGE_URL);
                imageThinking = new Image(imageInput);

                imageInput = new FileInputStream(EATING_IMAGE_URL);
                imageEating  = new Image(imageInput);

                imageInput = new FileInputStream(WAITING_IMAGE_URL);
                imageWaiting = new Image(imageInput);
                isInit = true;
            }
            TextArea textArea = (TextArea) LaunchMain.simServices.lookup("#log_area");
            textArea.setEditable(false);
            for(int i= 0;i<vector.size();i++){
                textArea.appendText(vector.elementAt(i));
                textArea.appendText("\n");
            }
            //1.更新哲学家状态,及其附属的叉子状态
            for(int i = 0;i<philosophers.length;i++){
                ImageView imageViewPhi = (ImageView) LaunchMain.simServices.lookup("#phi_"+(philosophers[i].getPhiId()+1));
                ImageView imageViewPhiLeftFork = (ImageView) LaunchMain.simServices.lookup("#phi_"+(philosophers[i].getPhiId()+1)+"_left");
                ImageView imageViewPhiRightFork = (ImageView) LaunchMain.simServices.lookup("#phi_"+(philosophers[i].getPhiId()+1)+"_right");
                if(philosophers[i].getStates() == PhilosopherStates.EATING){
                    imageViewPhi.setImage(imageEating);
                }else if(philosophers[i].getStates() == PhilosopherStates.THINKING){
                    imageViewPhi.setImage(imageThinking);
                }else if(philosophers[i].getStates() == PhilosopherStates.WAITING){
                    imageViewPhi.setImage(imageWaiting);
                }
                imageViewPhiRightFork.setVisible(philosophers[i].hasRightFork);
                imageViewPhiLeftFork.setVisible(philosophers[i].hasLeftFork);
            }
            //3.更新新桌子上叉子状态
            for(int i =0;i< forks.length;i++){
                ImageView imageViewFork = (ImageView)LaunchMain.simServices.lookup("#fork_"+(i+1));
                if(forks[i].getLock().availablePermits()>0){
                    imageViewFork.setVisible(true);
                    continue;
                }
                imageViewFork.setVisible(false);
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }




}
